/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.parent.service.impl;

import com.fanrenqiji.entity.ChannelEntity;
import com.fanrenqiji.modules.business.channel.dao.ChannelDao;
import com.fanrenqiji.modules.business.channel.service.ChannelService;
import com.fanrenqiji.modules.business.channel.vo.ChannelVO;
import com.fanrenqiji.modules.sys.entity.Role;
import com.fanrenqiji.modules.sys.entity.User;
import com.fanrenqiji.modules.sys.utils.UserUtils;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.ParentEntity;
import com.fanrenqiji.modules.business.parent.vo.ParentVO;
import com.fanrenqiji.modules.business.parent.dao.ParentDao;
import com.fanrenqiji.modules.business.parent.service.ParentService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 家长信息表业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "parent")
@Transactional(readOnly = true)
public class ParentServiceImpl implements ParentService {

    @Autowired
    private ParentDao parentDao;
    @Autowired
    private ChannelDao channelDao;

    @Autowired
    DozerBeanMapper beanMapper;

    /**
     * 插入
     *
     * @param parent 插入的对象
     * @return 插入数据条数
     */
    @Transactional
    public ParentVO insert(ParentVO parent) {
        ParentEntity parentEntity = new ParentEntity();
        beanMapper.map(parent, parentEntity);
        parentDao.save(parentEntity);
        beanMapper.map(parentEntity, parent);
        return parent;
    }


    /**
     * 批量插入
     *
     * @param list 数据列表
     * @return 插入数据条数
     */
    @Transactional
    public void insertList(List<ParentVO> list) {
        for (ParentVO parent : list) {
            insert(parent);
        }
    }


    /**
     * 修改
     *
     * @param parent 修改的数据
     * @return 修改的数据
     */
    @Transactional
    public ParentVO update(ParentVO parent) {
        ParentEntity parentEntity = null;
        if (StringUtils.isNotEmpty(parent.getId())) {
            parentEntity = parentDao.findOne(Long.parseLong(parent.getId()));
        }
        if (parentEntity == null) {
            parentEntity = new ParentEntity();
        }
        beanMapper.map(parent, parentEntity);
        parentDao.clear();
        parentDao.save(parentEntity);
        beanMapper.map(parentEntity, parent);
        return parent;
    }


    /**
     * 删除
     *
     * @param id 数据标识
     * @return 删除的数据条数
     */
    @Transactional
    public void delete(String id) {
        parentDao.deleteById(Long.parseLong(id));
    }


    /**
     * 查询所有数据
     *
     * @return 数据列表
     */
    public List<ParentVO> getAll() {
        List<ParentVO> list = Lists.newArrayList();
        List<ParentEntity> entities = parentDao.findAllList();
        for (ParentEntity parentEntity : entities) {
            ParentVO parentVO = new ParentVO();
            beanMapper.map(parentEntity, parentVO);
            list.add(parentVO);
        }
        return list;
    }


    /**
     * 通过条件查询数据列表
     *
     * @param parent 查询条件
     * @return 数据列表
     */
    public List<ParentVO> getList(ParentVO parent) {
        List<ParentVO> list = Lists.newArrayList();
        DetachedCriteria dc = parentDao.createDetachedCriteria();
        dc.add(Restrictions.eq(ParentEntity.DEL_FLAG, ParentEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("parent", "parent"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(parent.getName())) {
        dc.add(Restrictions.like("name", "%" + parent.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(parent.getName())) {
        dc.add(Restrictions.eq("name", parent.getName()));
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));

    */
        List<ParentEntity> entities = parentDao.find(dc);
        for (ParentEntity parentEntity : entities) {
            ParentVO parentVO = new ParentVO();
            beanMapper.map(parentEntity, parentVO);
            list.add(parentVO);
        }
        return list;
    }


    /**
     * 通过id查询数据
     *
     * @param id 数据标识
     * @return 数据对象
     */
    public ParentVO get(String id) {
        ParentVO parent = new ParentVO();
        ParentEntity parentEntity = parentDao.findOne(Long.parseLong(id));
        if (parentEntity == null) {
            parentEntity = new ParentEntity();
        }
        beanMapper.map(parentEntity, parent);
        return parent;
    }


    /**
     * 根据条件分页查询
     *
     * @param page
     * @param parent
     * @return
     */
    public Page<ParentVO> findParentByPage(Page<ParentVO> page, ParentVO parent) {
        Page<ParentEntity> pageEntity = new Page<ParentEntity>(page.getPageNo(), page.getPageSize());
        beanMapper.map(page, pageEntity);
        DetachedCriteria dc = parentDao.createDetachedCriteria();
        dc.add(Restrictions.eq(ParentEntity.DEL_FLAG, ParentEntity.DEL_FLAG_NORMAL));


        if (!StringUtils.isNotEmpty(page.getOrderBy())) {
            dc.addOrder(Order.desc("id"));
        }
        pageEntity = parentDao.find(pageEntity, dc);
        beanMapper.map(pageEntity, page);
        List<ParentVO> list = Lists.newArrayList();
        for (ParentEntity parentEntity : pageEntity.getList()) {
            ParentVO parentVO = new ParentVO();
            beanMapper.map(parentEntity, parentVO);
            list.add(parentVO);
        }
        page.setList(list);

        return page;
    }

    /**
     * 根据 身份证号 获取 ParentVO
     *
     * @param idcode
     * @return
     */
    public ParentVO getByidcode(String idcode) {
        ParentEntity parentEntity = parentDao.getByidcode(idcode);
        if (parentEntity == null) return null;
        ParentVO parentVO = new ParentVO();
        beanMapper.map(parentEntity, parentVO);
        return parentVO;
    }

    /**
     * 根据 身份证号 获取 ParentVO
     *
     * @param idcode
     * @return
     */
    public ParentVO getByPhone(String idcode) {
        ParentEntity parentEntity = parentDao.getByPhone(idcode);
        if (parentEntity == null) return null;
        ParentVO parentVO = new ParentVO();
        beanMapper.map(parentEntity, parentVO);
        return parentVO;
    }

}
